﻿using System;
using System.Drawing;

using HsTec.Mobile.Green.BLL;
using HsTec.Mobile.Green.Util;
using Baimasoft.Manage.SqlServerDAL;

public partial class ReportAnalysis_AuthenticationBenchmark : System.Web.UI.Page
{
    ForecastBusinessBLL bll = new ForecastBusinessBLL();

    HsTec.Mobile.Green.DAL.ForecastBusinessDAL MyFB = new HsTec.Mobile.Green.DAL.ForecastBusinessDAL();

    Param MyP = new Param();

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((string)Session["Baimasoft_Pers_ID"] == null)
        {
            Response.Redirect("../Logon.aspx");
        }
        if (!IsPostBack)
        {
            BindsltProvince();
            txtYearsDataBind();
        }
    }
    protected void SaveData_ServerClick(object sender, EventArgs e)
    {
        int iType = 0;
        //1.获取某月全省单位业务量耗电警戒值A
        string sYear = txtYears.SelectedValue.Trim();
        string sNowYear = DateTime.Now.ToString("yyyyMM");
        if (sYear == sNowYear)
            iType = 1;
        //1.1 获取某月的调整比例
        string sProportion = bll.GetProportion(sYear, sltProvince.Value.Trim());
        if (String.IsNullOrEmpty(sProportion))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('该月份调整比例未录入！');</script>");
            return;
        }
        string sA = bll.GetA(sYear, sltProvince.Value.Trim());
        if (String.IsNullOrEmpty(sA))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('全省规划单位业务量耗电值未录入！');</script>");
            return;
        }
        Decimal tmpA = Decimal.Parse(sA) * Decimal.Parse(sProportion);
        lblA.Text = tmpA.ToString("0.00") + " (度/erl)";
        //1.2 该月各地市预测业务量
        string sService = String.Empty;
        //1.3 全省预测耗电量
        //1.3.1 通过上月实际话务量
        string sPowers = String.Empty;
        int iBack = 0;
        if (iType == 0)
        {
            iBack = bll.GetServiceByFact(sltProvince.Value.Trim(), sYear, ref sService);
            if (iBack == 1)
            {
                sService = Convert.ToString(Math.Round(Convert.ToDecimal(sService) / 10000, 2));
                lblArea.Text = sService;
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + sService + "该月的话音业务量、PDCH值未录入！');</script>");
                return;
            }
            iBack = bll.GetPowersByFact(sltProvince.Value.Trim(), sYear, ref sPowers);
            if (iBack == 1)
            {
                //省公司
                string sPower = bll.GetProvincePowers(sYear);
                if (sPower.Trim() == "" || sPower == null)
                {
                    sPower = "0";
                }
                sPowers = Convert.ToString(Math.Round((Convert.ToDecimal(sPowers)) / 10000 + Convert.ToDecimal(sPower), 2));
                lblPowers.Text = sPowers;
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + sPowers + "该月的单位业务量耗电标杆值和本月业务量值未录入！');</script>");
                return;
            }
        }
        else
        {
            iBack = bll.GetServiceByForecast(sltProvince.Value.Trim(), sYear, ref sService);
            if (iBack == 1)
            {
                sService = Convert.ToString(Math.Round(Convert.ToDecimal(sService) / 10000, 2));
                lblArea.Text = sService;
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + sService + "该月的预测话音业务量、预测PDCH值未录入！');</script>");
                return;
            }
            iBack = bll.GetPowersByForecast(sltProvince.Value.Trim(), sYear, ref sPowers);
            if (iBack == 1)
            {
                //string sPower = bll.GetProvincePowers(sYear);
                sPowers = Convert.ToString(Math.Round((Convert.ToDecimal(sPowers)) / 10000, 2));

                lblPowers.Text = sPowers;
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + sPowers + "该月的单位业务量耗电标杆值和本月预测业务量值未录入！');</script>");
                return;
            }
        }
        //1.4 全省预测单位业务量耗电值B
        Decimal tmpB = Decimal.Parse(sPowers);
        //前一个月累计实际耗电量值
        string sCumulativePowers = String.Empty;
        sCumulativePowers = bll.GetCumulativePowers(sltProvince.Value.Trim(), sYear);
        labCumulativePowers.Text = sCumulativePowers;
        lblPowertmp.Text = sPowers;
        sPowers = Convert.ToString(Math.Round(Convert.ToDecimal(sPowers) + Convert.ToDecimal(sCumulativePowers), 2));
        lblPowers.Text = sPowers;
        //累计业务量值
        string sCumulativeService = String.Empty;
        sCumulativeService = bll.GetCumulativeService(sltProvince.Value.Trim(), sYear);
        labCumulativeService.Text = sCumulativeService;
        tmpB = Convert.ToDecimal(sPowers) / Convert.ToDecimal(sCumulativeService);
        lblB.Text = tmpB.ToString("0.00") + " (度/erl)";
        if (tmpA >= tmpB)
            lblResults.Text = "全省单位业务量耗电警戒值A>=全省预测单位业务量耗电值B";
        else
        {
            lblResults.Text = "全省单位业务量耗电警戒值A<全省预测单位业务量耗电值B";
            lblResults.ForeColor = Color.Red;
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('警告：警戒值A<预测耗电量值B！');</script>");
        }
    }
    private void BindsltProvince()
    {
        sltProvince.DataTextField = "Name";
        sltProvince.DataValueField = "ID";
        sltProvince.DataSource = bll.GetProvince();
        sltProvince.DataBind();
        sltProvince.Items.Insert(0, "请选择县市区域...");
        sltProvince.Items[0].Value = "";
    }
    private void txtYearsDataBind()
    {
        txtYears.DataTextField = "Name";
        txtYears.DataValueField = "Name";
        txtYears.DataSource = MyP.GetCodeByParentCode("1");
        txtYears.DataBind();
        txtYears.Items.Insert(0, "请选择...");
        txtYears.Items[0].Value = "";
    }
}
